package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.Cacheable;

import java.util.List;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口")
@Slf4j
public class SetMealController
{
    @Autowired
    private SetMealService setMealService;

    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames="setMealCache",key = "#setmealDTO.categoryId")
    //@CachePut(cacheNames = "setMealCache",key = "#result.categoryId")//如果返回值是setmeal的话用这种写法也可以
    public Result saveWithDish(@RequestBody SetmealDTO setmealDTO)
    {
        setMealService.saveWithDish(setmealDTO);
        return Result.success();
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询套餐")//用于修改的时候回显
    public Result<SetmealVO> getByIdwithDish(@PathVariable long id)
    {
        SetmealVO setmealVO=setMealService.getByIdwithDish(id);
        return Result.success(setmealVO);
    }
    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "套餐分页查询接口")
    public Result<PageResult> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO)
    {
        PageResult pageResult=setMealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    @PostMapping("/status/{status}")
    @ApiOperation(value = "修改套餐起售、停售状态接口")
    @CacheEvict(cacheNames="setMealCache",allEntries = true)
    public Result startOrstop(@PathVariable Integer status,long id)
    {
        setMealService.startOrstop(status,id);
        return Result.success();
    }

    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation(value = "批量删除套餐")
    @CacheEvict(cacheNames="setMealCache",allEntries = true)
    public Result delete(@RequestParam List<Long> ids)
    {
        setMealService.deleteBatch(ids);
        return Result.success();
    }

    @PutMapping
    @ApiOperation(value = "修改套餐接口")
    @CacheEvict(cacheNames="setMealCache",allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO)
    {
        setMealService.updateWithDish(setmealDTO);
        return Result.success();
    }
}
